package com.derive.education_service.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Admin;
import com.derive.education_service.entity.Role;
import com.derive.education_service.mapper.AdminMapper;
import com.derive.education_service.mapper.RoleMapper;
import com.derive.education_service.web.admin.bean.dto.RoleAddDto;
import com.derive.education_service.web.admin.bean.dto.RoleUpDto;
import com.derive.education_service.web.admin.bean.vo.AdminInfoVo;
import com.derive.education_service.web.admin.service.RoleService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 角色权限管理操作
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 新增角色权限 -- 超管
     * @return
     */
    @Override
    public ResponseMessage addRole(HttpServletRequest request, RoleAddDto roleAddDto) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        // 实体获取
        Role role = new Role();
        BeanUtils.copyProperties(roleAddDto, role);
        // 数据填充
        role.setAdminId(adminInfoVo.getAdminId());
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        // 数据插入
        int row = roleMapper.insert(role);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("角色权限添加失败");
        } else {
            return ResponseMessage. createBySuccessMessage("角色权限添加成功");
        }
    }

    /**
     * 删除角色权限
     * 删除之前先判断是否存在用户具备该角色且超管不能删除
     * 超管
     * @return
     */
    @Override
    public ResponseMessage delRole(HttpServletRequest request, int roleId) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        // 校验权限是否存在正在使用的用户
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        Admin admin = adminMapper.selectList(queryWrapper).get(0);
        if (admin != null) {
            return ResponseMessage.createByErrorMessage("角色占用中，无法删除");
        }
        // 超管角色和默认账号无法删除
        if (roleId == 1 || roleId == 3) {
            return ResponseMessage.createByErrorMessage("该角色无法删除");
        }
        // 删除权限
        int row = roleMapper.deleteById(roleId);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("删除权限失败");
        }
        return ResponseMessage.createBySuccessMessage("删除权限成功");
    }

    /**
     * 查询平台角色信息 -- 不分页 -- 超管
     * @return
     */
    @Override
    public ResponseMessage getRoleList(HttpServletRequest request) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        // 获取全部角色信息
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> roleList = roleMapper.selectList(queryWrapper);

        return ResponseMessage.createBySuccess(roleList);
    }

    /**
     * 编辑平台角色信息 -- 描述信息 -- 超管
     * @return
     */
    @Override
    public ResponseMessage upRole(HttpServletRequest request, RoleUpDto roleUpDto) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        // 校验是否具备操作权限
        if (adminInfoVo.getRoleId() != 1) {
            return ResponseMessage.createByErrorMessage("对不起，你没有该操作权限");
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleUpDto, role);
        role.setUpdateTime(new Date());

        int row = roleMapper.updateById(role);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("编辑失败");
        }
        return ResponseMessage.createBySuccessMessage("编辑成功");
    }

    /**
     * 获取当前账号角色权限
     * @return
     */
    @Override
    public ResponseMessage getAdminRole(HttpServletRequest request) {
        // 转换数据
        ObjectMapper objectMapper = new ObjectMapper();
        AdminInfoVo adminInfoVo = objectMapper
                .convertValue(request.getSession().getAttribute(request.getHeader("USESSIONID")), AdminInfoVo.class);
        Role role = roleMapper.selectById(adminInfoVo.getRoleId());
        return ResponseMessage.createBySuccess(role);
    }
}
